/*
   Copyright (c) 2011, 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-05 Marco Riccardi - initial implementation
 *	2012-04-14 Marco Riccardi - added Reduction Registrations
 *  2015-01-11 Marco Riccardi - added Property Registrations
 *
 */
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.SortedMap;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

import org.mizartools.system.Article;
import org.mizartools.system.ArticleFactory;
import org.mizartools.system.ListArticleFile;
import org.mizartools.system.VocabularyFile;

@SuppressWarnings("serial")
public class ArticlesFrame extends JInternalFrame implements Runnable {

	private JTextPane textPane;
	private JPanel panel;
	private JScrollPane scroller;
	private JProgressBar progressBar;
	private MainFrame mainFrame;
	
	public ArticlesFrame(MainFrame mainFrame) {
    	super("Articles", true, true, true, true);
    	this.mainFrame = mainFrame;
    	panel = new JPanel();
    	panel.setLayout(new BorderLayout());
		panel.setPreferredSize(new Dimension(500, 350));
		textPane = new JTextPane();
		textPane.addHyperlinkListener(createHyperLinkListener()); 
        scroller = new JScrollPane();
		scroller.getViewport().add(textPane);
		panel.add(scroller, BorderLayout.CENTER);
		getContentPane().add(panel, BorderLayout.CENTER);
		progressBar = new JProgressBar();
		pack();
	}
	
    public HyperlinkListener createHyperLinkListener() { 
     	return new HyperlinkListener() { 
     		@Override
     	    public void hyperlinkUpdate(HyperlinkEvent e) { 
     		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 
     		    if (e instanceof HTMLFrameHyperlinkEvent) { 
     		    	((HTMLDocument)textPane.getDocument()).processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)e); 
     		    } else if (e instanceof HyperlinkEvent) {
     		    	String command = ((HyperlinkEvent)e).getDescription();
     		    	if (command.startsWith("Constructors+")){
         		    	mainFrame.showConstructor(command.replaceFirst("Constructors\\+", ""));
     		    	}
     		    	if (command.startsWith("Notations+")){
         		    	mainFrame.showNotation(command.replaceFirst("Notations\\+", ""));
     		    	}
     		    	if (command.startsWith("Definientia+")){
         		    	mainFrame.showDefinientia(command.replaceFirst("Definientia\\+", ""));
     		    	}
     		    	if (command.startsWith("Registrations+")){
         		    	mainFrame.showRegistration(command.replaceFirst("Registrations\\+", ""));
     		    	}
     		    	if (command.startsWith("IdentifyRegistrations+")){
         		    	mainFrame.showIdentifyRegistration(command.replaceFirst("IdentifyRegistrations\\+", ""));
     		    	}
     		    	if (command.startsWith("PropertyRegistrations+")){
         		    	mainFrame.showPropertyRegistration(command.replaceFirst("PropertyRegistrations\\+", ""));
     		    	}
     		    	if (command.startsWith("ReductionRegistrations+")){
         		    	mainFrame.showReductionRegistration(command.replaceFirst("ReductionRegistrations\\+", ""));
     		    	}
     		    	if (command.startsWith("Theorems+")){
         		    	mainFrame.showTheorem(command.replaceFirst("Theorems\\+", ""));
     		    	}
     		    	if (command.startsWith("Schemes+")){
         		    	mainFrame.showScheme(command.replaceFirst("Schemes\\+", ""));
     		    	}
     		    } 
     		} 
     	    }
     	};
    } 	
	
	private String getHtml(){
    	StringBuilder html = new StringBuilder();
    	SortedMap<String, Integer> sortedArticles = ListArticleFile.getAllArticleMap();
    	progressBar.setMaximum(sortedArticles.size()+1);
    	progressBar.setValue(0);
    	progressBar.setIndeterminate(false);
    	html.append("<html>");
    	html.append("<body>");
    	html.append("<table width=100% border=1>");
		html.append("<tr>");
		html.append("<td>ARTICLEID</td>");
		html.append("<td>Library Order</td>");
		html.append("<td>Vocabularies</td>");
		html.append("<td>Constructors</td>");
		html.append("<td>Definientia</td>");
		html.append("<td>Identify Registrations</td>");
		html.append("<td>Property Registrations</td>");
		html.append("<td>Reduction Registrations</td>");
		html.append("<td>Notations</td>");
		html.append("<td>Registrations</td>");
		html.append("<td>Requirements</td>");
		html.append("<td>Schemes</td>");
		html.append("<td>Theorems</td>");
		html.append("</tr>");
    	Article article = null;
    	int i = 0;
    	for (String aid : sortedArticles.keySet()){
        	progressBar.setValue(i++);
        	progressBar.repaint();
    		article = ArticleFactory.getInstance().getArticle(aid);
			html.append("<tr>");
    		html.append("<td>");
        	html.append("<font face=\"monospace\"><b>");
        	html.append(article.getAid());
        	html.append("</b></font");
    		html.append("</td>");
			html.append("<td>"+sortedArticles.get(aid).toString()+"</td>");
    		if (VocabularyFile.hasVocabulary(article.getAid()))
    			html.append("<td>Y</td>");
    		else
    			html.append("<td></td>");
    		if (article.hasConstructors())
    			html.append("<td><a href=\"Constructors+"+article.getAid()+"\">con</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasDefinientia())
    			html.append("<td><a href=\"Definientia+"+article.getAid()+"\">def</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasIdentifyRegistrations())
    			html.append("<td><a href=\"IdentifyRegistrations+"+article.getAid()+"\">ide</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasPropertyRegistrations())
    			html.append("<td><a href=\"PropertyRegistrations+"+article.getAid()+"\">dpr</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasReductionRegistrations())
    			html.append("<td><a href=\"ReductionRegistrations+"+article.getAid()+"\">drd</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasNotations())
    			html.append("<td><a href=\"Notations+"+article.getAid()+"\">not</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasRegistrations())
    			html.append("<td><a href=\"Registrations+"+article.getAid()+"\">reg</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasRequirements())
    			html.append("<td>Y</td>");
    		else
    			html.append("<td></td>");
    		if (article.hasSchemes())
    			html.append("<td><a href=\"Schemes+"+article.getAid()+"\">sch</a></td>");
    		else
    			html.append("<td></td>");
    		if (article.hasTheorems())
    			html.append("<td><a href=\"Theorems+"+article.getAid()+"\">the</a></td>");
    		else
    			html.append("<td></td>");
			html.append("</tr>");
    	}
    	html.append("</table>");
    	html.append("</body>");
    	html.append("</html>");
    	return html.toString();
	}

	@Override
	public void run() {
    	progressBar.setIndeterminate(true);
    	getContentPane().add(progressBar, BorderLayout.SOUTH);
		panel.setVisible(false);
		textPane.setContentType("text/html");
		textPane.setText(getHtml());
    	progressBar.setIndeterminate(true);
		textPane.setEditable(false);
		getContentPane().remove(progressBar);
		panel.setVisible(true);
	}
}
